package com.crm.service.impl.opportunityManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.opportunityManager.OpportunityContactMapper;
import com.crm.dao.mapper.opportunityManager.OpportunityProductMapper;
import com.crm.model.entity.opportunityManager.OpportunityContact;
import com.crm.model.entity.opportunityManager.OpportunityContactExample;
import com.crm.model.entity.opportunityManager.OpportunityProduct;
import com.crm.model.entity.opportunityManager.OpportunityProductExample;
import com.crm.service.opportunityManager.OpportunityContactDbService;
import com.crm.service.opportunityManager.OpportunityProductDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商机产品
 */
@Service
public class OpportunityContactDbServiceImpl implements OpportunityContactDbService {

    @Autowired
    private OpportunityContactMapper opportunityContactMapper;

    /**
     * 商机-商机详情-查询当前商机联系人
     * @param opportunityId 商机id
     * @param order 分组
     * @return
     */
    @Override
    public List<OpportunityContact> opportunityContactList(String opportunityId, String order) {
        OpportunityContactExample opportunityContactExample = new OpportunityContactExample();
        OpportunityContactExample.Criteria criteria = opportunityContactExample.createCriteria();
        opportunityContactExample.setOrderByClause(order);
        criteria.andOpportunityIdEqualTo(opportunityId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<OpportunityContact> list = opportunityContactMapper.selectByExample(opportunityContactExample);
        return list;
    }

    /**
     * 商机管理-商机详情-新增联系人到商机联系人下
     * @param opportunityContact 商机联系人实体
     * @return
     */
    @Override
    public Boolean addOpportunityContact(OpportunityContact opportunityContact) {
        Boolean result = false;
        int num = opportunityContactMapper.insertSelective(opportunityContact);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 查询同一个商机下联系人只能有一个首要联系人
     * @param opportunityId 商机id
     * @param contactType 联系人类型 0首要联系人  1次要联系人
     * @return
     */
    @Override
    public List<OpportunityContact> getOpportunityAndContactType(String opportunityId, Integer contactType) {
        OpportunityContactExample opportunityContactExample = new OpportunityContactExample();
        OpportunityContactExample.Criteria criteria = opportunityContactExample.createCriteria();
        criteria.andOpportunityIdEqualTo(opportunityId);
        criteria.andContactTypeEqualTo(contactType);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<OpportunityContact> list = opportunityContactMapper.selectByExample(opportunityContactExample);
        return list;
    }

    /**
     * 商机管理-商机详情-修改商机联系人
     * @param contact 商机联系人实体
     * @return
     */
    @Override
    public Boolean updateOpportunityContact(OpportunityContact contact) {
        Boolean result = false;
        int num = opportunityContactMapper.updateByPrimaryKeySelective(contact);
        if(num>0){
            result = true;
        }
        return result;
    }
}
